package com.hnucm.house;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.hnucm.house.bean.HomeTop.SearchViewActivity;
import com.hnucm.house.ui.fragment.HomeFragment;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private static final int INDEX_HOMEPAGE = 0;
    private static final int INDEX_VIEW = 1;
    private static final int INDEX_FOLLOW = 2;
    private static final int INDEX_MESSAGE = 3;
    private static final int INDEX_ME = 4;

    private SparseArray<Fragment> mFragmentSparseArray = new SparseArray<>();

    private Fragment mCurrenFragment;

    private Fragment mLastFragment;

    private int mLastIndex = -1;

    private Context mContext;

    Unbinder mUnbinder;

    @BindView(R.id.navigation_bottom)
    BottomNavigationView mBottomNavigationView;

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        //recreate时保存当前页面位置
        outState.putInt("index",mLastIndex);
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        //恢复recreate前的页面
        mLastIndex = savedInstanceState.getInt("index");
        switchFragment(mLastIndex);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton imageButton =  findViewById(R.id.search);
        mContext = getApplicationContext();
        mUnbinder = ButterKnife.bind(this);
        initBottomNavigation();
        //判断当前是recreate还是新启动
        if(savedInstanceState == null){
            switchFragment(INDEX_HOMEPAGE);
        }
        //设置沉浸式状态栏
        QMUIStatusBarHelper.translucent(this);
    }

    public void onclick(View view){
        Intent intent = new Intent(MainActivity.this,SearchViewActivity.class);
        startActivity(intent);
    }
    private void initBottomNavigation() {
        mBottomNavigationView.setOnNavigationItemSelectedListener(
                menuItem ->{
                    switch (menuItem.getItemId()){
                        case R.id.menu_home:
                            switchFragment(INDEX_HOMEPAGE);
                            return true;
                        case R.id.menu_view:
                            switchFragment(INDEX_VIEW);
                            return true;
                        case R.id.menu_follow:
                            switchFragment(INDEX_FOLLOW);
                            return true;
                        case R.id.menu_message:
                            switchFragment(INDEX_MESSAGE);
                            return true;
                        case R.id.menu_me:
                            switchFragment(INDEX_ME);
                            return true;
                        default:
                            return false;
                    }
                }
        );
    }


    private void switchFragment(int index) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //将当前显示的fragment和上一个需要隐藏的frament分别加上tag，并获取出来
        //给fragment加上tag，这样可以通过findFragmentByTag找到存在的fragment
        mCurrenFragment = fragmentManager.findFragmentByTag("fragment" + index);
        mLastFragment = fragmentManager.findFragmentByTag("fragment" + mLastIndex);
        //如果位置不同
        if(index != mLastIndex){
            if(mLastFragment != null){
                transaction.hide(mLastFragment);
            }
            if(mCurrenFragment == null){
                mCurrenFragment = getFragment(index);
                transaction.add(R.id.big_container,mCurrenFragment,"fragment" + index);
            } else {
                transaction.show(mCurrenFragment);
            }
        }

        //如果位置相同或新启动的应用
        if(index == mLastIndex){
            if(mCurrenFragment == null){
                mCurrenFragment = getFragment(index);
                transaction.add(R.id.big_container,mCurrenFragment,"framgent" + index);
            }
        }

        transaction.commit();
        mLastIndex = index;
    }

    private Fragment getFragment(int index){
        Fragment fragment = mFragmentSparseArray.get(index);
        if(fragment == null){
            switch (index){
                case INDEX_HOMEPAGE:
                    fragment = HomeFragment.getInstance();
                    break;
                case INDEX_VIEW:
                    fragment = HomeFragment.getInstance();
                    break;
                case INDEX_FOLLOW:
                    fragment = HomeFragment.getInstance();
                    break;
                case INDEX_MESSAGE:
                    fragment = HomeFragment.getInstance();
                    break;
                case INDEX_ME:
                    fragment = HomeFragment.getInstance();
                    break;
                default:
                    break;
            }
            mFragmentSparseArray.put(index,fragment);
        }
        return fragment;
    }

}